import pandas as pd

# 读取CSV文件
df = pd.read_csv('data.csv')
# df_top300 = df.head(300)

# 确保“总分”列存在且数据类型正确
if '销量' in df.columns and pd.api.types.is_numeric_dtype(df['销量']):
    # 计算平均值、最大值和最小值
    average_score = df['销量'].mean()
    max_score = df['销量'].max()
    min_score = df['销量'].min()
    median_score = df['销量'].median()
    variance_score = df['销量'].var()

    # 格式化平均值和方差为两位小数
    formatted_average_score = "{:.2f}".format(average_score)
    formatted_variance_score = "{:.2f}".format(variance_score)

    # 打印统计信息
    print(f"平均值: {formatted_average_score}")
    print(f"最大值: {max_score}")
    print(f"最小值: {min_score}")
    print(f"中位数: {median_score}")
    print(f"方差: {formatted_variance_score}")
else:
    print("CSV文件中不存在‘销量’列")

# 将这些统计信息保存到CSV文件中
stats = {'平均值': [average_score], '最大值': [max_score], '最小值': [min_score], '中位数':[median_score], '方差':[variance_score]}
stats_df = pd.DataFrame(stats)
stats_df.to_csv('分组求值.csv', index=False, encoding='utf-8-sig')

